# ticket_system.py

import random
import pymysql
import datetime

class TicketSystem:
    def __init__(self, host, port, user, password, database):
        self.db = pymysql.connect(host=host, port=port, user=user, password=password)
        self.cur = self.db.cursor()
        self.cur.execute(f"USE {database}")
        self.business = ["取款", "存款", "挂失", "还贷"]

    def take_num(self, user_id, business, status):
        sql = "INSERT INTO ticket_num (user_id, business, status, arrival_time) VALUES (%s, %s, %s, %s)"
        val = (user_id, business, status, datetime.datetime.now())
        self.cur.execute(sql, val)
        self.db.commit()

    def del_all(self,table_name):
        sql = f"TRUNCATE TABLE {table_name}"
        self.cur.execute(sql)
        self.db.commit()

    def insert_all(self):
        for i in range(1, 10):
            user_id = i
            business = random.choice(self.business)
            status = random.randint(0, 2)
            self.take_num(user_id, business, status)

    def query_all(self):
        self.cur.execute("SELECT * FROM ticket_num")
        res = self.cur.fetchall()
        for row in res:
            print(row)

    def close_connection(self):
        self.cur.close()
        self.db.close()
